package com.yylending.las.tinker.network;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by fuqiang on 2017/7/27.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class TinkerPatchPresenter extends Presenter<TinkerPatchModel, TinkerPatchPresenter.View> {
	private static final String TAG = "TinkerPatchPresenter";

	public static class Generator implements PresenterFactory<TinkerPatchPresenter> {
		@Override
		public TinkerPatchPresenter create() {
			return new TinkerPatchPresenter();
		}
	}

	public interface View extends IView {
		void getPatchSuccess(TinkerPatchModel.ResultBean resultBean);

		void getPatchFailed();
	}

	private View mView;

	@Override
	public void attach(TinkerPatchPresenter.View view) {
		mView = view;
		mModel = new TinkerPatchModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getPatch(long time) {
		execute(new TinkerPatchModel.Params(time), FUNCTIONAL_CODE, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void subAccountResult(TinkerPatchModel.Result result) {
		if (result != null && BaseResultEntity.isResponseSuccess(result) && null != result.getData()) {
			mView.getPatchSuccess(result.getData());
		} else {
			mView.getPatchFailed();
		}
	}
}
